package cloudspace.vm;

import org.apache.log4j.Logger;
import org.zkoss.zhtml.Br;
import org.zkoss.zhtml.Div;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Label;

import cloudspace.vm.io.console.CometEvent;
import cloudspace.vm.io.console.CometExecutor;


/**
 * The Class ConfigDisplay. This class is the configuration display module. The
 * desktop logging mechanism does not work properly.
 */
public class ConfigDisplay extends Div
{
    protected Logger logger = Logger.getLogger( ConfigDisplay.class );

    /**
	 * 
	 */
    private static final long serialVersionUID = -6771478623478042975L;


    /**
     * This listener is linked to a Comet thread to be checked and updated when
     * needed.
     * 
     */
    private class numDesktopListener extends CometEvent
    {

        /** The desktop label. */
        Label desktopLabel;


        /**
         * Instantiates a new num desktop listener.
         * 
         * @param numDeskLab
         *            The label that stores the number of desktops to be updated
         */
        public numDesktopListener( Label numDeskLab )
        {
            desktopLabel = numDeskLab;
        }


        /*
         * (non-Javadoc)
         * 
         * @see cloudspace.vm.io.console.CometEventListener#checkEvent()
         */
        public void run()
        {
            try
            {
                prepare();
                int numDesk = 0/* VM.currentVM().getNumberDesktops() */;
                int storedDesk = Integer.parseInt( desktopLabel.getValue() );
                if ( numDesk != storedDesk )
                {
                    desktopLabel.setValue( "" + numDesk );
                    desktopLabel.invalidate();
                }
                cleanup();
            }
            catch ( InterruptedException e )
            {
                logger.debug( "The configuration display component's comet runnable was interupted.",
                    e );
                e.printStackTrace();
            }
        }
    }


    /**
     * On creation of the display this method is run. It is invoked by zk.
     */
    public void onCreate()
    {
        Executions.getCurrent().getDesktop().enableServerPush( true );
        Label timeStampLabel = new Label( "Classes are current as of:  "
            + VM.currentVM().getLoaderTime() );
        this.appendChild( timeStampLabel );
        this.appendChild( new Br() );
        this.appendChild( new Br() );
        Label numDesktopsLabel = new Label( "Number of users: " );
        Label boundNumber = new Label( "" );
        boundNumber.setValue( Integer.toString( /*
                                                 * VM.currentVM().getNumberDesktops
                                                 * ())
                                                 */0 ) );
        CometExecutor.addListener( new numDesktopListener( boundNumber ) );
        this.appendChild( numDesktopsLabel );
        this.appendChild( boundNumber );
        this.appendChild( new Br() );
    }
}
